package gui;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import net.BackClient;
import net.ReceiveInformation;
import util.Parser;
import util.Protocol;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7331169995258952629L;

	private JPanel contentPane;
	
	BackClient backClient = new BackClient();

	Protocol protocol = new Protocol();
	
	Parser parser = new Parser(); 

	String nickname;
	
	String friendnickname;
	
	private JTextField textField;
	
	JTextArea textArea;
	
	Thread thread = new Thread(new ReceiveInformation());
	
	ReceiveInformation information = new ReceiveInformation();
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserUI frame = new UserUI();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	public UserUI() 
	{
		thread.start();
		setTitle("聊天界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 650, 449);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 43, 614, 169);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		Font f = new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 20);
		textArea.setFont(f);
		scrollPane.setViewportView(textArea);
		
		JButton sendbutton = new JButton("发送");
		sendbutton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				String friendname = friendnickname;
				String conversation = textField.getText();
				String from = "From;" +nickname + ",";
				String to = "To;" + friendname + ",";
				String massage = from + to + conversation;
				
				textArea.append(nickname + ":" + conversation + "\n");
				textField.setText("");
				
				String finalmassage = protocol.getToFriendmassage(massage);
				backClient.SendMassage(finalmassage);
			}
		});
		sendbutton.setBounds(531, 362, 93, 23);
		contentPane.add(sendbutton);
		
		JButton exitNewButton = new JButton("关闭");
		exitNewButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				backClient.SendMassage("close");
				setVisible(false);
			}
		});
		exitNewButton.setBounds(378, 362, 93, 23);
		contentPane.add(exitNewButton);
		
		textField = new JTextField();
		textField.setBounds(10, 222, 614, 116);
		contentPane.add(textField);
		textField.setColumns(10);
		
		contentPane.setVisible(true);
	}
	
	public void setNickname(String nickname)
	{
		this.nickname = nickname;
	}
	
	public void getfrinedID(String friendID)
	{
		this.friendnickname = friendID;
		information.addthisWindows(friendnickname, this);
		JLabel lblNewLabel_1 = new JLabel(friendnickname);
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 20));
		lblNewLabel_1.setBounds(10, 18, 54, 15);
		contentPane.add(lblNewLabel_1);
		lblNewLabel_1.setVisible(true);
	}
	
	public void setConversation(String name,String content)
	{
		textArea.append(name + ":" +content + "\n");
	}
}


